using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace BW.Mes.WinForms.BasicData
{
    public partial class PagerControl : UserControl
    {
        public event EventHandler PageChanged;

        public PagerControl()
        {
            InitializeComponent();
            comboPageSize.SelectedIndex = 0;
        }

        private int _pageIndex = 1;
        private int _pageSize = 10;
        private int _totalCount = 0;

        [Browsable(true)]
        [Category("分页")]
        public int PageIndex
        {
            get => _pageIndex;
            set { _pageIndex = value; UpdatePager(); }
        }
        [Browsable(true)]
        [Category("分页")]
        public int PageSize
        {
            get => _pageSize;
            set { _pageSize = value; UpdatePager(); }
        }
        [Browsable(true)]
        [Category("分页")]
        public int TotalCount
        {
            get => _totalCount;
            set { _totalCount = value; UpdatePager(); }
        }
        public int TotalPage => (_totalCount + _pageSize - 1) / _pageSize;

        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (_pageIndex != 1)
            {
                _pageIndex = 1;
                PageChanged?.Invoke(this, EventArgs.Empty);
                UpdatePager();
            }
        }
        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (_pageIndex > 1)
            {
                _pageIndex--;
                PageChanged?.Invoke(this, EventArgs.Empty);
                UpdatePager();
            }
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (_pageIndex < TotalPage)
            {
                _pageIndex++;
                PageChanged?.Invoke(this, EventArgs.Empty);
                UpdatePager();
            }
        }
        private void btnLast_Click(object sender, EventArgs e)
        {
            if (_pageIndex != TotalPage)
            {
                _pageIndex = TotalPage;
                PageChanged?.Invoke(this, EventArgs.Empty);
                UpdatePager();
            }
        }
        private void btnGoToPage_Click(object sender, EventArgs e)
        {
            if (int.TryParse(txtGoToPage.Text.Trim(), out int goToPage))
            {
                if (goToPage >= 1 && goToPage <= TotalPage)
                {
                    _pageIndex = goToPage;
                    PageChanged?.Invoke(this, EventArgs.Empty);
                    UpdatePager();
                }
                else
                {
                    MessageBox.Show($"请输入1到{TotalPage}之间的页码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请输入有效的页码数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void comboPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.TryParse(comboPageSize.SelectedItem.ToString(), out int size))
            {
                _pageSize = size;
                _pageIndex = 1;
                PageChanged?.Invoke(this, EventArgs.Empty);
                UpdatePager();
            }
        }
        private void UpdatePager()
        {
            lblPage.Text = $"页码: {_pageIndex} / {TotalPage}";
            lblTotal.Text = $"共 {_totalCount} 条记录";
            btnFirst.Enabled = _pageIndex > 1;
            btnPrev.Enabled = _pageIndex > 1;
            btnNext.Enabled = _pageIndex < TotalPage;
            btnLast.Enabled = _pageIndex < TotalPage;
            btnGoToPage.Enabled = TotalPage > 1;
            txtGoToPage.Enabled = TotalPage > 1;
        }
    }
} 